package prepare.huawei;

import java.util.*;

/**
 * description: <a href="https://blog.csdn.net/weixin_44052055/article/details/123996124">高效的任务规划</a>
 *
 * @author <a href="zhaoyingling12@163.com">simon zhao</a>
 * @since 2023/3/25
 */
public class EfficientTaskPlanning {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        // 有多少组任务
        int n = Integer.parseInt(input.nextLine());
        List<int[][]> groupList = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            int len = Integer.parseInt(input.nextLine());
            int[][] g = new int[len][2];
            for (int j = 0; j < len; j++) {
                String[] ss = input.nextLine().split(" ");
                g[j][0] = Integer.parseInt(ss[0]);
                g[j][1] = Integer.parseInt(ss[1]);
            }
            groupList.add(g);
        }

        for (int[][] group : groupList) {
            System.out.println(getMinTime(group));
        }
    }

    private static int getMinTime(int[][] group) {
        Arrays.sort(group, (o1, o2) -> -Integer.compare(o1[1], o2[1]));
        int time = 0, restTime = 0;
        for (int[] ints : group) {
            int leftTime = restTime - ints[0];
            // 小于0则表示时间不够用
            if (leftTime < 0) {
                time += Math.abs(leftTime) + ints[1];
            } else {
                // 额外还需多长时间
                time += Math.abs(leftTime - ints[1]);
            }
            restTime = ints[1];
        }
        return time;
    }
}
